/**   
* @Title: PrivilegeServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月3日 上午9:17:54 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.OperateService;
import com.nebula.upms.service.biz.PrivilegeService;
import com.nebula.upms.service.model.po.*;
import com.nebula.upms.service.support.constant.UpmsLocalFinal;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nebula.common.framework.BaseService;


/** 
* @ClassName: PrivilegeServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author zhanghui
* @date 2016年2月3日 上午9:17:54 
*  
*/
@Service("privilegeService")
@Transactional
public class PrivilegeServiceImpl extends BaseService implements PrivilegeService {
	@Autowired
	private OperateService operateService;
	/*
	 * <p>Title: getFullPrivilegeTree</p>
	 * <p>Description: </p>
	 * @return
	 * @see com.nebula.upms.service.PrivilegeService#getFullPrivilegeTree()
	 */
	@Override
	public String getFullPrivilegeTree() {
		ObjectMapper mapper = new ObjectMapper();  
	    ObjectNode rootNode = mapper.createObjectNode();
	    rootNode.put("id", "ROOT");
	    rootNode.put("text", "权限树");
	    rootNode.put("iconCls", "fa fa-th-large");
	    rootNode.put("programCode", "");
	    rootNode.put("appName", "");
	    rootNode.put("operateJSON","");
	    rootNode.put("priority", "0");
	    rootNode.put("type", "ROOT");
	    ArrayNode rootArrayNode = mapper.createArrayNode ();  
	    UpmsAppSystemPOExample UpmsAppSystemPOExample=new UpmsAppSystemPOExample();
	    UpmsAppSystemPOExample.setOrderByClause(" priority ASC");
	    List<UpmsAppSystemPO> UpmsAppSystemPOList= UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsAppSystemPOExample);
	    for (UpmsAppSystemPO UpmsAppSystemPO : UpmsAppSystemPOList) {
	    	ObjectNode node=mapper.createObjectNode();
    		node.put("id", UpmsAppSystemPO.getAppSystemId());
    		node.put("text", UpmsAppSystemPO.getName());
    		node.put("iconCls", "fa "+UpmsAppSystemPO.getIcon());
    		node.put("state", "open");
    		node.put("programCode", "");
    		node.put("appName", "");
    		node.put("operateJSON","");
    		node.put("priority", UpmsAppSystemPO.getPriority());
    		node.put("type", "APPSYSTEM");
    		ArrayNode arrayNode = mapper.createArrayNode (); 
    		UpmsPrivilegePOExample UpmsPrivilegePOExample=new UpmsPrivilegePOExample();
    		UpmsPrivilegePOExample.createCriteria().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePOExample.setOrderByClause(" priority ASC");
    		List<UpmsPrivilegePO> UpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsPrivilegePOExample);
    		
    		
    		UpmsPrivilegePOExample.clear();
    		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePO rootUpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);
    		if(null!=rootUpmsPrivilegePO){
    			this.buildPrivilegeTree(node, arrayNode, rootUpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList);
    			rootNode.put("children",arrayNode);
    		}
    		rootArrayNode.add(node);
		}
	    rootNode.put("children", rootArrayNode);
	    ArrayNode basicArrayNode = mapper.createArrayNode (); 
	    if(UpmsAppSystemPOList.size()>0){
	    	basicArrayNode.add(rootNode);
	    }
	    String json="";
	    try {
			json=mapper.writeValueAsString(basicArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	private void buildPrivilegeTree(ObjectNode pNode,ArrayNode pChildrenNode,String pid,ObjectMapper mapper,
			List<UpmsPrivilegePO> UpmsPrivilegePOList){
		List<UpmsPrivilegePO> childrenList=this.getPrivilegeChildrenList(UpmsPrivilegePOList, pid);
		if(childrenList.size()>0){
			ArrayNode childrenNode = mapper.createArrayNode (); 
			for (UpmsPrivilegePO UpmsPrivilegePO : childrenList) {
				UpmsAppPO UpmsAppPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppPO.class, UpmsPrivilegePO.getAppId());
				
				
				Map<String,Object> params=new HashMap<>();
				params.put("privilegeId", UpmsPrivilegePO.getPrivilegeId());
				List<UpmsOperatePO> operatePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeHaveOperatePOList", params);
				
				String operateJSON="";
				try {
					operateJSON=mapper.writeValueAsString(operatePOList);
				} catch (JsonProcessingException e) {
					e.printStackTrace();
				}
				
				
				ObjectNode node=mapper.createObjectNode();
				node.put("id", UpmsPrivilegePO.getPrivilegeId());
	    		node.put("text", UpmsPrivilegePO.getName());
	    		node.put("iconCls", "fa "+UpmsPrivilegePO.getIcon());
	    		node.put("state", "open");
	    		node.put("programCode", UpmsPrivilegePO.getProgramCode());
	    		node.put("appName", UpmsAppPO.getName());
	    		node.put("operateJSON",operateJSON);
	    		node.put("priority", UpmsPrivilegePO.getPriority());
	    		node.put("type", "PRIVILEGE");
	    		this.buildPrivilegeTree(node, childrenNode, UpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList);
	    		pNode.put("children",childrenNode);
				
			}
		}
		pChildrenNode.add(pNode);
	}
	
	private void buildPrivilegeGroupPrivilegeTree(ObjectNode pNode,ArrayNode pChildrenNode,String pid,ObjectMapper mapper,
			List<UpmsPrivilegePO> UpmsPrivilegePOList,String privilegeGroupId,List<String> privilegeGroupIdList){
		List<UpmsPrivilegePO> childrenList=this.getPrivilegeChildrenList(UpmsPrivilegePOList, pid);
		if(childrenList.size()>0){
			ArrayNode childrenNode = mapper.createArrayNode (); 
			for (UpmsPrivilegePO UpmsPrivilegePO : childrenList) {
				UpmsAppPO UpmsAppPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppPO.class, UpmsPrivilegePO.getAppId());
				
				
				Map<String,Object> params=new HashMap<>();
				params.put("privilegeId", UpmsPrivilegePO.getPrivilegeId());
				if(StringUtils.isNotEmpty(privilegeGroupId)){
					params.put("privilegeGroupId", privilegeGroupId);
				}
				if(null!=privilegeGroupIdList&&privilegeGroupIdList.size()>0){
					params.put("privilegeGroupIdList", privilegeGroupIdList);
				}
				
				List<UpmsOperatePO> operatePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupHaveOperatePOList", params);
				
				String operateJSON="";
				try {
					operateJSON=mapper.writeValueAsString(operatePOList);
				} catch (JsonProcessingException e) {
					e.printStackTrace();
				}
				
				
				ObjectNode node=mapper.createObjectNode();
				node.put("id", UpmsPrivilegePO.getPrivilegeId());
	    		node.put("text", UpmsPrivilegePO.getName());
	    		node.put("iconCls", "fa "+UpmsPrivilegePO.getIcon());
	    		node.put("state", "open");
	    		node.put("programCode", UpmsPrivilegePO.getProgramCode());
	    		node.put("appName", UpmsAppPO.getName());
	    		node.put("operateJSON",operateJSON);
	    		node.put("priority", UpmsPrivilegePO.getPriority());
	    		node.put("type", "PRIVILEGE");
	    		this.buildPrivilegeGroupPrivilegeTree(node, childrenNode, UpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,privilegeGroupId,privilegeGroupIdList);
	    		pNode.put("children",childrenNode);
				
			}
		}
		pChildrenNode.add(pNode);
	}
	
	private List<UpmsPrivilegePO> getPrivilegeChildrenList(List<UpmsPrivilegePO> UpmsPrivilegePOList ,String pid){
		List<UpmsPrivilegePO> list=new ArrayList<>();
		for (UpmsPrivilegePO UpmsPrivilegePO : UpmsPrivilegePOList) {
			if(pid.equals(UpmsPrivilegePO.getPrivilegePid())){
				list.add(UpmsPrivilegePO);
			}
		}
		return list;
	}

	/*
	 * <p>Title: getUpmsPrivilegePOByKey</p>
	 * <p>Description: </p>
	 * @param privilegeId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeService#getUpmsPrivilegePOByKey(java.lang.String)
	 */
	@Override
	public UpmsPrivilegePO getUpmsPrivilegePOByKey(String privilegeId) {
		UpmsPrivilegePO po=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPrivilegePO.class, privilegeId);
		if(null==po){
			UpmsAppSystemPO UpmsAppSystemPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppSystemPO.class, privilegeId);
			UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
			example.createCriteria().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId())
			.andPrivilegePidIsNull();
			po=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example);
			if(null==po){//无根节点，创建新的根节点
				po=new UpmsPrivilegePO();
				po.setPrivilegeId(UUIDGenerator.random32UUID());
				po.setName("ROOT");
				po.setAppSystemId(UpmsAppSystemPO.getAppSystemId());
				po.setCreateTime(new Date().getTime());
				po.setType(UpmsLocalFinal.PRIVILEGE_TYPE_Y0A);
				po.setPriority(0);
				UpmsDs.instance().mybatis().ds().mapper().insert(po);
			}
			
		}
		return po;
	}

	/*
	 * <p>Title: saveUpmsPrivilegePO</p>
	 * <p>Description: </p>
	 * @param UpmsPrivilegePO
	 * @param operateNameArray
	 * @param operateCodeArray
	 * @see com.nebula.upms.service.PrivilegeService#saveUpmsPrivilegePO(com.hbdiy.common.model.po.UpmsPrivilegePO, java.lang.String[], java.lang.String[])
	 */
	@Override
	public void saveUpmsPrivilegePO(UpmsPrivilegePO UpmsPrivilegePO, String[] operateNameArray, String[] operateCodeArray) {
		int priority=0;
		UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
		example.createCriteria().andPrivilegePidEqualTo(UpmsPrivilegePO.getPrivilegePid());
		example.setOrderByClause(" priority DESC");
		
		UpmsPrivilegePO last=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example);
		if(null!=last){
			priority=last.getPriority()+1;
		}
		
		UpmsPrivilegePO.setPrivilegeId(UUIDGenerator.random32UUID());
		UpmsPrivilegePO.setType(UpmsLocalFinal.PRIVILEGE_TYPE_Y0B);
		UpmsPrivilegePO.setCreateTime(new Date().getTime());
		UpmsPrivilegePO.setIsDelete(false);
		UpmsPrivilegePO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().insert(UpmsPrivilegePO);
		
		
		if(StringUtils.isNotEmpty(UpmsPrivilegePO.getProgramCode())){
			UpmsOperatePOExample UpmsOperatePOExample =new UpmsOperatePOExample();
			UpmsOperatePOExample.createCriteria().andTypeEqualTo(UpmsLocalFinal.OPERATE_TYPE_Y10);
			UpmsOperatePOExample.setOrderByClause(" type,create_time asc ");
			List<UpmsOperatePO> UpmsOperatePOList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsOperatePOExample);
			for (UpmsOperatePO UpmsOperatePO : UpmsOperatePOList) {
				UpmsPrivilegeOperatePOKey pokey=new UpmsPrivilegeOperatePOKey();
				pokey.setOperateId(UpmsOperatePO.getOperateId());
				pokey.setPrivilegeId(UpmsPrivilegePO.getPrivilegeId());
				UpmsDs.instance().mybatis().ds().mapper().insert(pokey);
			}
			if(null!=operateNameArray){
				for (int i = 0; i < operateNameArray.length; i++) {
					UpmsOperatePO UpmsOperatePO=new UpmsOperatePO();
					UpmsOperatePO.setOperateId(UUIDGenerator.random32UUID());
					UpmsOperatePO.setName(operateNameArray[i]);
					UpmsOperatePO.setCode(operateCodeArray[i]);
					UpmsOperatePO.setType(UpmsLocalFinal.OPERATE_TYPE_Y11);
					UpmsOperatePO.setCreateTime(new Date().getTime());
					UpmsDs.instance().mybatis().ds().mapper().insert(UpmsOperatePO);
					
					UpmsPrivilegeOperatePOKey pokey=new UpmsPrivilegeOperatePOKey();
					pokey.setOperateId(UpmsOperatePO.getOperateId());
					pokey.setPrivilegeId(UpmsPrivilegePO.getPrivilegeId());
					UpmsDs.instance().mybatis().ds().mapper().insert(pokey);
				}
			}
			
		}
	}

	/*
	 * <p>Title: updateUpmsPrivilegePO</p>
	 * <p>Description: </p>
	 * @param UpmsPrivilegePO
	 * @param operateNameArray
	 * @param operateCodeArray
	 * @see com.nebula.upms.service.PrivilegeService#updateUpmsPrivilegePO(com.hbdiy.common.model.po.UpmsPrivilegePO, java.lang.String[], java.lang.String[])
	 */
	@Override
	public void updateUpmsPrivilegePO(UpmsPrivilegePO UpmsPrivilegePO, String[] operateNameArray, String[] operateCodeArray) {
		UpmsPrivilegePO.setUpdateTime(new Date().getTime());
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsPrivilegePO);
		
		if(StringUtils.isNotEmpty(UpmsPrivilegePO.getProgramCode())){
			UpmsPrivilegeOperatePOExample example=new UpmsPrivilegeOperatePOExample();
			example.createCriteria().andPrivilegeIdEqualTo(UpmsPrivilegePO.getPrivilegeId());
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example);
			
			
			UpmsOperatePOExample UpmsOperatePOExample =new UpmsOperatePOExample();
			UpmsOperatePOExample.createCriteria().andTypeEqualTo(UpmsLocalFinal.OPERATE_TYPE_Y10);
			UpmsOperatePOExample.setOrderByClause("type,create_time asc");
			List<UpmsOperatePO> UpmsOperatePOList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsOperatePOExample);
			for (UpmsOperatePO UpmsOperatePO : UpmsOperatePOList) {
				UpmsPrivilegeOperatePOKey pokey=new UpmsPrivilegeOperatePOKey();
				pokey.setOperateId(UpmsOperatePO.getOperateId());
				pokey.setPrivilegeId(UpmsPrivilegePO.getPrivilegeId());
				UpmsDs.instance().mybatis().ds().mapper().insert(pokey);
			}
			if(null!=operateNameArray){
				for (int i = 0; i < operateNameArray.length; i++) {
					UpmsOperatePO UpmsOperatePO=new UpmsOperatePO();
					UpmsOperatePO.setOperateId(UUIDGenerator.random32UUID());
					UpmsOperatePO.setName(operateNameArray[i]);
					UpmsOperatePO.setCode(operateCodeArray[i]);
					UpmsOperatePO.setType(UpmsLocalFinal.OPERATE_TYPE_Y11);
					UpmsOperatePO.setCreateTime(new Date().getTime());
					UpmsDs.instance().mybatis().ds().mapper().insert(UpmsOperatePO);
					
					UpmsPrivilegeOperatePOKey pokey=new UpmsPrivilegeOperatePOKey();
					pokey.setOperateId(UpmsOperatePO.getOperateId());
					pokey.setPrivilegeId(UpmsPrivilegePO.getPrivilegeId());
					UpmsDs.instance().mybatis().ds().mapper().insert(pokey);
				}
			}
			
		}else{
			UpmsPrivilegeOperatePOExample example=new UpmsPrivilegeOperatePOExample();
			example.createCriteria().andPrivilegeIdEqualTo(UpmsPrivilegePO.getPrivilegeId());
			UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example);
			
		}
		
	}

	/*
	 * <p>Title: moveup</p>
	 * <p>Description: </p>
	 * @param privilegeId
	 * @see com.nebula.upms.service.PrivilegeService#moveup(java.lang.String)
	 */
	@Override
	public void moveup(String privilegeId) {
		UpmsPrivilegePO UpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPrivilegePO.class,privilegeId);
		UpmsPrivilegePOExample example =new UpmsPrivilegePOExample();
		example.createCriteria().andPrivilegePidEqualTo(UpmsPrivilegePO.getPrivilegePid())
		.andAppSystemIdEqualTo(UpmsPrivilegePO.getAppSystemId());
		example.setOrderByClause("priority ASC");
		List<UpmsPrivilegePO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		int index=0;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getPrivilegeId().equals(privilegeId)){
				if(i==0){
					index=0;
				}else{
					index=i-1;
				}
				break;
			}
		}
		UpmsPrivilegePO prev=list.get(index);
		int priority=prev.getPriority();
		prev.setPriority(UpmsPrivilegePO.getPriority());
		UpmsPrivilegePO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(prev);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsPrivilegePO);
		
	}

	/*
	 * <p>Title: movedown</p>
	 * <p>Description: </p>
	 * @param privilegeId
	 * @see com.nebula.upms.service.PrivilegeService#movedown(java.lang.String)
	 */
	@Override
	public void movedown(String privilegeId) {
		UpmsPrivilegePO UpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPrivilegePO.class,privilegeId);
		UpmsPrivilegePOExample example =new UpmsPrivilegePOExample();
		example.createCriteria().andPrivilegePidEqualTo(UpmsPrivilegePO.getPrivilegePid())
		.andAppSystemIdEqualTo(UpmsPrivilegePO.getAppSystemId());
		example.setOrderByClause("priority ASC");
		List<UpmsPrivilegePO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		int index=0;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getPrivilegeId().equals(privilegeId)){
				if(i==(list.size()-1)){
					index=list.size()-1;
				}else{
					index=i+1;
				}
				break;
			}
		}
		UpmsPrivilegePO prev=list.get(index);
		int priority=prev.getPriority();
		prev.setPriority(UpmsPrivilegePO.getPriority());
		UpmsPrivilegePO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(prev);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsPrivilegePO);
		
	}

	/*
	 * <p>Title: removePrivilege</p>
	 * <p>Description: </p>
	 * @param privilegeId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeService#removePrivilege(java.lang.String)
	 */
	@Override
	public boolean removePrivilege(String privilegeId) {
		UpmsPrivilegeGroupPrivilegePOExample UpmsPrivilegeGroupPrivilegePOExample=new UpmsPrivilegeGroupPrivilegePOExample();
		UpmsPrivilegeGroupPrivilegePOExample.createCriteria().andPrivilegeIdEqualTo(privilegeId);
		int gcount=(int)UpmsDs.instance().mybatis().ds().mapper().countByExample(UpmsPrivilegeGroupPrivilegePOExample);
		
		UpmsStaffPrivatePrivilegePOExample UpmsStaffPrivatePrivilegePOExample=new UpmsStaffPrivatePrivilegePOExample();
		UpmsStaffPrivatePrivilegePOExample.createCriteria().andPrivilegeIdEqualTo(privilegeId);
		int scount=(int)UpmsDs.instance().mybatis().ds().mapper().countByExample(UpmsStaffPrivatePrivilegePOExample);
		if(gcount+scount>0){
			return false;
		}
		this.deletePrivilege(privilegeId);
		return true;
	}
	
	private void deletePrivilege(String privilegeId ){
		UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
		example.createCriteria().andPrivilegePidEqualTo(privilegeId);
		List<UpmsPrivilegePO> list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
		if(list.size()>0){
			for (UpmsPrivilegePO UpmsPrivilegePO : list) {
				deletePrivilege(UpmsPrivilegePO.getPrivilegeId());
			}
		}
		UpmsPrivilegeOperatePOExample UpmsPrivilegeOperatePOExample=new UpmsPrivilegeOperatePOExample();
		UpmsPrivilegeOperatePOExample.createCriteria().andPrivilegeIdEqualTo(privilegeId);
		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(UpmsPrivilegeOperatePOExample);
		
		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsPrivilegePO.class,privilegeId);
	}
	
	/*
	 * <p>Title: privilegeTreeJson</p>
	 * <p>Description: </p>
	 * @param privilegeGroupId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeGroupService#privilegeTreeJson(java.lang.String)
	 */
	@Override
	public String getPrivilegeGroupPrivilegeTreeJson(String privilegeGroupId) {
		Map<String,Object> params=new HashMap<>();
		params.put("privilegeGroupId", privilegeGroupId);
		
		ObjectMapper mapper = new ObjectMapper();  
	    ObjectNode rootNode = mapper.createObjectNode();
	    rootNode.put("id", "ROOT");
	    rootNode.put("text", "权限树");
	    rootNode.put("iconCls", "fa fa-th-large");
	    rootNode.put("programCode", "");
	    rootNode.put("operateJSON","");
	    rootNode.put("appName", "");
	    rootNode.put("priority", "0");
	    rootNode.put("type", "ROOT");
	    ArrayNode rootArrayNode = mapper.createArrayNode ();  
	    
	    List<UpmsAppSystemPO> UpmsAppSystemPOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupAppSystemList", params);
	    for (UpmsAppSystemPO UpmsAppSystemPO : UpmsAppSystemPOList) {
	    	ObjectNode node=mapper.createObjectNode();
    		node.put("id", UpmsAppSystemPO.getAppSystemId());
    		node.put("text", UpmsAppSystemPO.getName());
    		node.put("iconCls", "fa "+UpmsAppSystemPO.getIcon());
    		node.put("state", "open");
    		node.put("programCode", "");
    		node.put("operateJSON","");
    		node.put("appName", "");
    		node.put("priority", UpmsAppSystemPO.getPriority());
    		node.put("type", "APPSYSTEM");
    		ArrayNode arrayNode = mapper.createArrayNode (); 
    		List<UpmsPrivilegePO> UpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupPrivilegeList", params);
    		
    		
    		UpmsPrivilegePOExample UpmsPrivilegePOExample =new  UpmsPrivilegePOExample();
    		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePO rootUpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);
    		if(null!=rootUpmsPrivilegePO){
    			this.buildPrivilegeGroupPrivilegeTree(node, arrayNode, rootUpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,privilegeGroupId,null);
    			rootNode.put("children",arrayNode);
    		}
    		rootArrayNode.add(node);
		}
	    rootNode.put("children", rootArrayNode);
	    ArrayNode basicArrayNode = mapper.createArrayNode (); 
	    if(UpmsAppSystemPOList.size()>0){
	    	basicArrayNode.add(rootNode);
	    }
	    String json="";
	    try {
			json=mapper.writeValueAsString(basicArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
		
	}
	
	@Override
	public String getPrivilegeGroupPrivilegeTreeJson(List<String> privilegeGroupIdList) {
		Map<String,Object> params=new HashMap<>();
		params.put("privilegeGroupIdList", privilegeGroupIdList);
		
		ObjectMapper mapper = new ObjectMapper();  
	    ObjectNode rootNode = mapper.createObjectNode();
	    rootNode.put("id", "ROOT");
	    rootNode.put("text", "权限树");
	    rootNode.put("iconCls", "fa fa-th-large");
	    rootNode.put("programCode", "");
	    rootNode.put("operateJSON","");
	    rootNode.put("appName", "");
	    rootNode.put("priority", "0");
	    rootNode.put("type", "ROOT");
	    ArrayNode rootArrayNode = mapper.createArrayNode ();  
	    
	    List<UpmsAppSystemPO> UpmsAppSystemPOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupAppSystemList", params);
	    for (UpmsAppSystemPO UpmsAppSystemPO : UpmsAppSystemPOList) {
	    	ObjectNode node=mapper.createObjectNode();
    		node.put("id", UpmsAppSystemPO.getAppSystemId());
    		node.put("text", UpmsAppSystemPO.getName());
    		node.put("iconCls", "fa "+UpmsAppSystemPO.getIcon());
    		node.put("state", "open");
    		node.put("programCode", "");
    		node.put("operateJSON","");
    		node.put("appName", "");
    		node.put("priority", UpmsAppSystemPO.getPriority());
    		node.put("type", "APPSYSTEM");
    		ArrayNode arrayNode = mapper.createArrayNode (); 
    		List<UpmsPrivilegePO> UpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupPrivilegeList", params);
    		
    		
    		UpmsPrivilegePOExample UpmsPrivilegePOExample =new  UpmsPrivilegePOExample();
    		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePO rootUpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);
    		if(null!=rootUpmsPrivilegePO){
    			this.buildPrivilegeGroupPrivilegeTree(node, arrayNode, rootUpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,null,privilegeGroupIdList);
    			rootNode.put("children",arrayNode);
    		}
    		rootArrayNode.add(node);
		}
	    rootNode.put("children", rootArrayNode);
	    ArrayNode basicArrayNode = mapper.createArrayNode (); 
	    if(UpmsAppSystemPOList.size()>0){
	    	basicArrayNode.add(rootNode);
	    }
	    String json="";
	    try {
			json=mapper.writeValueAsString(basicArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
		
	}
	

	/*
	 * <p>Title: getPrivilegeChildren</p>
	 * <p>Description: </p>
	 * @param privilegeId
	 * @return
	 * @see com.nebula.upms.service.PrivilegeService#getPrivilegeChildren(java.lang.String)
	 */
	@Override
	public String getPrivilegeChildren(String privilegeId) {
		List<UpmsPrivilegePO> list=new ArrayList<>();
		
		UpmsPrivilegePO UpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsPrivilegePO.class, privilegeId);
		if(null==UpmsPrivilegePO){
			UpmsAppSystemPO UpmsAppSystemPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppSystemPO.class, privilegeId);
			if(null==UpmsAppSystemPO){
				UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
				list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
				UpmsAppSystemPOExample appSystemPOExample=new UpmsAppSystemPOExample();
				List<UpmsAppSystemPO> ls=UpmsDs.instance().mybatis().ds().mapper().selectByExample(appSystemPOExample);
				for (UpmsAppSystemPO apo : ls) {
					UpmsPrivilegePO ppo=new UpmsPrivilegePO();
					ppo.setPrivilegeId(apo.getAppSystemId());
					list.add(ppo);
				}
				
			}else{
				UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
				example.createCriteria().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
				list=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
			}
		}else{
			UpmsPrivilegePOExample example=new UpmsPrivilegePOExample();
			List<UpmsPrivilegePO> allList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(example);
			this.findChildrenList(privilegeId, allList, list);
			list.add(UpmsPrivilegePO);
		}
		ObjectMapper mapper = new ObjectMapper();
		String json="";
		try {
			 json=mapper.writeValueAsString(list);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	private void findChildrenList(String pid,List<UpmsPrivilegePO> allList,List<UpmsPrivilegePO> childrenList){
		for (UpmsPrivilegePO UpmsPrivilegePO : allList) {
			if(pid.equals(UpmsPrivilegePO.getPrivilegePid())){
				childrenList.add(UpmsPrivilegePO);
				this.findChildrenList(UpmsPrivilegePO.getPrivilegeId(), allList, childrenList);
			}
			
		}
		
	}

	@Override
	public String getPrivilegeGroupCheckPrivilegeTreeJson(String privilegeGroupId) {
		Map<String,Object> params=new HashMap<>();
		params.put("privilegeGroupId", privilegeGroupId);
		
		ObjectMapper mapper = new ObjectMapper();  
	    ObjectNode rootNode = mapper.createObjectNode();
	    rootNode.put("id", "ROOT");
	    rootNode.put("text", "权限树");
	    rootNode.put("iconCls", "fa fa-th-large");
	    rootNode.put("programCode", "");
	    rootNode.put("appName", "");
	    rootNode.put("operateJSON","");
	    rootNode.put("priority", "0");
	    rootNode.put("type", "ROOT");
	    rootNode.put("checked", true);
	    ArrayNode rootArrayNode = mapper.createArrayNode ();  
	    UpmsAppSystemPOExample UpmsAppSystemPOExample=new UpmsAppSystemPOExample();
	    UpmsAppSystemPOExample.setOrderByClause(" priority ASC");
	    List<UpmsAppSystemPO> UpmsAppSystemPOList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsAppSystemPOExample);
	    List<UpmsAppSystemPO> checkedUpmsAppSystemPOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupAppSystemList", params);
	    for (UpmsAppSystemPO UpmsAppSystemPO : UpmsAppSystemPOList) {
	    	ObjectNode node=mapper.createObjectNode();
    		node.put("id", UpmsAppSystemPO.getAppSystemId());
    		node.put("text", UpmsAppSystemPO.getName());
    		node.put("iconCls", "fa "+UpmsAppSystemPO.getIcon());
    		node.put("state", "open");
    		node.put("programCode", "");
    		node.put("appName", "");
    		node.put("operateJSON","");
    		node.put("priority", UpmsAppSystemPO.getPriority());
    		node.put("type", "APPSYSTEM");
    		boolean isChecked=false;
    		for (UpmsAppSystemPO checkedAppSystem : checkedUpmsAppSystemPOList) {
				if(checkedAppSystem.getAppSystemId().equals(UpmsAppSystemPO.getAppSystemId())){
					isChecked=true;
					break;
				}
			}
    		node.put("checked", isChecked);
    		
    		ArrayNode arrayNode = mapper.createArrayNode (); 
    		UpmsPrivilegePOExample UpmsPrivilegePOExample=new UpmsPrivilegePOExample();
    		UpmsPrivilegePOExample.createCriteria().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePOExample.setOrderByClause(" priority ASC");
    		List<UpmsPrivilegePO> UpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper().selectByExample(UpmsPrivilegePOExample);
    		
    		UpmsPrivilegePOExample.clear();
    		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		//params.put("appSystemId", UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePO rootUpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);//this.commDAO.initDAO("PrivilegeMapper").selectOne("selectPrivilegeGroupPrivilegeRoot", params);//
    		if(null!=rootUpmsPrivilegePO){
    			List<UpmsPrivilegePO> checkedUpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupPrivilegeList", params);
    			this.buildPrivilegeCheckTree(node, arrayNode, rootUpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,privilegeGroupId,checkedUpmsPrivilegePOList);
    			rootNode.put("children",arrayNode);
    		}
    		rootArrayNode.add(node);
		}
	    rootNode.put("children", rootArrayNode);
	    ArrayNode basicArrayNode = mapper.createArrayNode (); 
	    if(UpmsAppSystemPOList.size()>0){
	    	basicArrayNode.add(rootNode);
	    }
	    String json="";
	    try {
			json=mapper.writeValueAsString(basicArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	private void buildPrivilegeCheckTree(ObjectNode pNode,ArrayNode pChildrenNode,String pid,ObjectMapper mapper,
			List<UpmsPrivilegePO> UpmsPrivilegePOList,String privilegeGroupId,List<UpmsPrivilegePO> checkedUpmsPrivilegePOList){
		List<UpmsPrivilegePO> childrenList=this.getPrivilegeChildrenList(UpmsPrivilegePOList, pid);
		if(childrenList.size()>0){
			ArrayNode childrenNode = mapper.createArrayNode (); 
			for (UpmsPrivilegePO UpmsPrivilegePO : childrenList) {
				UpmsAppPO UpmsAppPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppPO.class, UpmsPrivilegePO.getAppId());
				
				
				Map<String,Object> params=new HashMap<>();
				params.put("privilegeId", UpmsPrivilegePO.getPrivilegeId());
				params.put("privilegeGroupId", privilegeGroupId);
				
				List<UpmsOperatePO> operatePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeHaveOperatePOList", params);
				List<UpmsOperatePO> checkedOperatePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectPrivilegeGroupHaveOperatePOList", params);
				List<UpmsOperatePO> operateJsonList=new ArrayList<>();
				for (UpmsOperatePO a : operatePOList) {
					boolean isChecked=false;
					for (UpmsOperatePO b : checkedOperatePOList) {
						if(a.getOperateId().equals(b.getOperateId())){
							isChecked=true;
							break;
						}
					}
					a.setIsDelete(isChecked);
					operateJsonList.add(a);
				}
				String operateJSON="";
				try {
					operateJSON=mapper.writeValueAsString(operateJsonList);
				} catch (JsonProcessingException e) {
					e.printStackTrace();
				}
				
				
				ObjectNode node=mapper.createObjectNode();
				node.put("id", UpmsPrivilegePO.getPrivilegeId());
	    		node.put("text", UpmsPrivilegePO.getName());
	    		node.put("iconCls", "fa "+UpmsPrivilegePO.getIcon());
	    		node.put("state", "open");
	    		node.put("programCode", UpmsPrivilegePO.getProgramCode());
	    		node.put("appName", UpmsAppPO.getName());
	    		node.put("operateJSON",operateJSON);
	    		node.put("priority", UpmsPrivilegePO.getPriority());
	    		node.put("type", "PRIVILEGE");
	    		boolean isChecked=false;
	    		for (UpmsPrivilegePO checkedUpmsPrivilegePO : checkedUpmsPrivilegePOList) {
					if(checkedUpmsPrivilegePO.getPrivilegeId().equals(UpmsPrivilegePO.getPrivilegeId())){
						isChecked=true;
						break;
					}
				}
	    		node.put("checked",isChecked);
	    		
	    		this.buildPrivilegeCheckTree(node, childrenNode, UpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,privilegeGroupId,checkedUpmsPrivilegePOList);
	    		pNode.put("children",childrenNode);
				
			}
		}
		pChildrenNode.add(pNode);
	}

	@Override
	public String getStaffPrivilegeTressJson(String staffId) {
		//selectStaffPrivilegeList
		Map<String,Object> params=new HashMap<>();
		params.put("staffId", staffId);
		
		ObjectMapper mapper = new ObjectMapper();  
	    ObjectNode rootNode = mapper.createObjectNode();
	    rootNode.put("id", "ROOT");
	    rootNode.put("text", "权限树");
	    rootNode.put("iconCls", "fa fa-th-large");
	    rootNode.put("programCode", "");
	    rootNode.put("operateJSON","");
	    rootNode.put("appName", "");
	    rootNode.put("priority", "0");
	    rootNode.put("type", "ROOT");
	    ArrayNode rootArrayNode = mapper.createArrayNode ();  
	    
	    List<UpmsAppSystemPO> UpmsAppSystemPOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectStaffAppSystemList", params);
	    for (UpmsAppSystemPO UpmsAppSystemPO : UpmsAppSystemPOList) {
	    	ObjectNode node=mapper.createObjectNode();
    		node.put("id", UpmsAppSystemPO.getAppSystemId());
    		node.put("text", UpmsAppSystemPO.getName());
    		node.put("iconCls", "fa "+UpmsAppSystemPO.getIcon());
    		node.put("state", "open");
    		node.put("programCode", "");
    		node.put("operateJSON","");
    		node.put("appName", "");
    		node.put("priority", UpmsAppSystemPO.getPriority());
    		node.put("type", "APPSYSTEM");
    		ArrayNode arrayNode = mapper.createArrayNode (); 
    		List<UpmsPrivilegePO> UpmsPrivilegePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectStaffPrivilegeList", params);
    		
    		
    		UpmsPrivilegePOExample UpmsPrivilegePOExample =new  UpmsPrivilegePOExample();
    		UpmsPrivilegePOExample.createCriteria().andPrivilegePidIsNull().andAppSystemIdEqualTo(UpmsAppSystemPO.getAppSystemId());
    		UpmsPrivilegePO rootUpmsPrivilegePO=UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(UpmsPrivilegePOExample);
    		if(null!=rootUpmsPrivilegePO){
    			this.buildStaffPrivilegeTree(node, arrayNode, rootUpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,staffId);
    			rootNode.put("children",arrayNode);
    		}
    		rootArrayNode.add(node);
		}
	    rootNode.put("children", rootArrayNode);
	    ArrayNode basicArrayNode = mapper.createArrayNode (); 
	    
	    if(UpmsAppSystemPOList.size()>0){
	    	basicArrayNode.add(rootNode);
	    }
	    String json="";
	    try {
			json=mapper.writeValueAsString(basicArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	private void buildStaffPrivilegeTree(ObjectNode pNode,ArrayNode pChildrenNode,String pid,ObjectMapper mapper,
			List<UpmsPrivilegePO> UpmsPrivilegePOList,String staffId){
		List<UpmsPrivilegePO> childrenList=this.getPrivilegeChildrenList(UpmsPrivilegePOList, pid);
		if(childrenList.size()>0){
			ArrayNode childrenNode = mapper.createArrayNode (); 
			for (UpmsPrivilegePO UpmsPrivilegePO : childrenList) {
				UpmsAppPO UpmsAppPO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsAppPO.class, UpmsPrivilegePO.getAppId());
				
				
				Map<String,Object> params=new HashMap<>();
				params.put("privilegeId", UpmsPrivilegePO.getPrivilegeId());
				params.put("staffId", staffId);
				List<UpmsOperatePO> operatePOList=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectList("selectStaffPrivilegeOperatePOList", params);
				
				String operateJSON="";
				try {
					operateJSON=mapper.writeValueAsString(operatePOList);
				} catch (JsonProcessingException e) {
					e.printStackTrace();
				}
				
				
				ObjectNode node=mapper.createObjectNode();
				node.put("id", UpmsPrivilegePO.getPrivilegeId());
	    		node.put("text", UpmsPrivilegePO.getName());
	    		node.put("iconCls", "fa "+UpmsPrivilegePO.getIcon());
	    		node.put("state", "open");
	    		node.put("programCode", UpmsPrivilegePO.getProgramCode());
	    		node.put("appName", UpmsAppPO.getName());
	    		node.put("operateJSON",operateJSON);
	    		node.put("priority", UpmsPrivilegePO.getPriority());
	    		node.put("type", "PRIVILEGE");
	    		this.buildStaffPrivilegeTree(node, childrenNode, UpmsPrivilegePO.getPrivilegeId(), mapper, UpmsPrivilegePOList,staffId);
	    		pNode.put("children",childrenNode);
				
			}
		}
		pChildrenNode.add(pNode);
	}
	
}
